import React from "react";
import PropTypes from "prop-types";
import HtmlContent from "@/components/HtmlContent";
import { Section, Input, Checkbox, ContextHelp } from "@/components/visualizations/editor";
import { createTextFormatter } from "@/lib/value-format";

function Editor({ column, onChange }) {
  return (
    <Section>
      <Input
        label={
          <React.Fragment>
            编码规则
            <ContextHelp.CoderFormatSpecs />
          </React.Fragment>
        }
        defaultValue={column.coderRule}
        onChange={event => onChange({ coderRule: event.target.value })}
      />
    </Section>
  );
}

Editor.propTypes = {
  column: PropTypes.shape({
    name: PropTypes.string.isRequired,
    allowHTML: PropTypes.bool,
    highlightLinks: PropTypes.bool,
  }).isRequired,
  onChange: PropTypes.func.isRequired,
};

export default function initCoderColumn(column) {
  const format = createTextFormatter(column.allowHTML && column.highlightLinks);

  function prepareData(row) {
    return {
      text: format(row[column.name]),
    };
  }

  function TextColumn({ row }) {
    // eslint-disable-line react/prop-types
    const { text } = prepareData(row);
    return column.allowHTML ? <HtmlContent>{text}</HtmlContent> : text;
  }

  TextColumn.prepareData = prepareData;

  return TextColumn;
}

initCoderColumn.friendlyName = "编码";
initCoderColumn.Editor = Editor;
